<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="主键" prop="id">
        <el-input v-model="dataForm.id" placeholder="主键"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="QQ号" prop="qq">
        <el-input v-model="dataForm.qq" placeholder="QQ号"></el-input>
      </el-form-item>
      <el-form-item label="公司名称" prop="company">
        <el-input v-model="dataForm.company" placeholder="公司名称"></el-input>
      </el-form-item>
      <el-form-item label="联系人" prop="contact">
        <el-input v-model="dataForm.contact" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item label="联系人职位" prop="duty">
        <el-input v-model="dataForm.duty" placeholder="联系人职位"></el-input>
      </el-form-item>
      <el-form-item label="公司电话" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="公司电话"></el-input>
      </el-form-item>
      <el-form-item label="公司传真" prop="fax">
        <el-input v-model="dataForm.fax" placeholder="公司传真"></el-input>
      </el-form-item>
      <el-form-item label="省编号" prop="provinceCode">
        <el-input v-model="dataForm.provinceCode" placeholder="省编号"></el-input>
      </el-form-item>
      <el-form-item label="市编号" prop="cityCode">
        <el-input v-model="dataForm.cityCode" placeholder="市编号"></el-input>
      </el-form-item>
      <el-form-item label="区编号" prop="districtCode">
        <el-input v-model="dataForm.districtCode" placeholder="区编号"></el-input>
      </el-form-item>
      <el-form-item label="省市区名称" prop="area">
        <el-input v-model="dataForm.area" placeholder="省市区名称"></el-input>
      </el-form-item>
      <el-form-item label="公司地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="公司地址"></el-input>
      </el-form-item>
      <el-form-item label="主营产品" prop="mainProduct">
        <el-input v-model="dataForm.mainProduct" placeholder="主营产品"></el-input>
      </el-form-item>
      <el-form-item label="录入用户数据的管理员ID" prop="userId">
        <el-input v-model="dataForm.userId" placeholder="录入用户数据的管理员ID"></el-input>
      </el-form-item>
      <el-form-item label="logo" prop="logo">
        <el-input v-model="dataForm.logo" placeholder="logo"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="dataForm.description" placeholder="描述"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils";
export default defineComponent({
  setup() {
    return reactive({
      visible: false,
      dataForm: {
        id: "",
        mobile: "",
        email: "",
        qq: "",
        company: "",
        contact: "",
        duty: "",
        phone: "",
        fax: "",
        provinceCode: "",
        cityCode: "",
        districtCode: "",
        area: "",
        address: "",
        mainProduct: "",
        userId: "",
        logo: "",
        description: ""
      }
    });
  },
  computed: {
    dataRule() {
      return {};
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      baseService.get("/noc/taccountcommon/" + this.dataForm.id).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.dataForm = res.data;
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }
        (!this.dataForm.id ? baseService.post : baseService.put)("/noc/taccountcommon", this.dataForm).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    }
  }
});
</script>
